package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

/**
 * @author ShuchaoZhang@edu.com
 * @since 2022/10/09 10:34
 */
@RestController
public class HelloController {

    @RequestMapping("gaopan/uploadFile")
    public BaseRespVo uploadFile(MultipartFile myfile) throws IOException {

        String originalFilename = myfile.getOriginalFilename();

        File file = new File("D:\\360Downloads", originalFilename);
        myfile.transferTo(file);

        return BaseRespVo.ok();
    }


    @RequestMapping("hello")
    public String hello(){
        return "hello world";
    }

    @RequestMapping("login")
    public BaseRespVo login(@CookieValue(value = "login",defaultValue = "") String login, String username, String password, HttpServletResponse response){

        if("zs".equals(username) && "123".equals(password)){
                Cookie cookie = new Cookie("login", username);
                cookie.setMaxAge(20*10);
                response.addCookie(cookie);
                return BaseRespVo.ok();
        }
        return BaseRespVo.error();
    }
}
